package com.lzy.netty.http;

import com.lzy.netty.http.HttpHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
*  初始化器，channel注册后，会触发初始化方法，完成业务逻辑
*/
public class HttpServerinitializer extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        // 通过SocketChannel获取ChannelPipeline
        ChannelPipeline channelPipeline = socketChannel.pipeline();

        /**
         * 添加自定义的handler，处理业务逻辑
         */
        // httpServerCodec 是有netty提供的助手类，类似于拦截器
        // 当请求到服务器，会经过HttpServerCodec解码器
        channelPipeline.addLast("httpServerCodec", new HttpServerCodec());

        // 添加自定义的助手类
        channelPipeline.addLast("httpHandler", new HttpHandler());
    }
}
